package com.hoperun.controller.admin;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AdminManager;
import com.hoperun.bean.AdminManagerRoles;
import com.hoperun.bean.AdminPermissions;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAdminManagerRolesSV;
import com.hoperun.service.IAdminManagerSV;
import com.hoperun.service.IAdminPermissionsSV;
import com.hoperun.utils.Constant;
import com.hoperun.utils.Constant.RecordState;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

	@Resource
	private IAdminManagerSV adminManagerSV;

	@Resource
	private IAdminPermissionsSV adminPermissionsSV;

	@Resource
	private IAdminManagerRolesSV adminManagerRolesSV;

	@RequestMapping
	public String index(final HttpServletRequest request, ModelMap modelMap) {
		try {
			initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin:welcome.welcome";
	}

	@RequestMapping("/preAuthor")
	public String preAuthority(String ids, final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			initMenus(request, modelMap);

			Criteria criteria = new Criteria();

			criteria.addEqual(AdminPermissions.COL_STATUS, RecordState.STS_USE,
					Criteria.AND);
			criteria.orderBy(AdminPermissions.COL_SORT);

			List<AdminPermissions> permissions = adminPermissionsSV
					.queryByCondition(criteria);

			criteria = new Criteria();
			criteria.addIn(AdminPermissions.COL_PERM_ID, ids, Criteria.AND);
			criteria.addNotEqual(AdminPermissions.COL_STATUS,
					RecordState.STS_DEL, Criteria.AND);
			criteria.limit(0, 1);

			List<AdminManager> adminManagers = adminManagerSV
					.queryByCondition(criteria);
			Set<String> resource = null;

			criteria = new Criteria(AdminManagerRoles.TABLE_PREFIX);
			criteria.addIn(AdminManagerRoles.COL_MANAGER_ID, ids, Criteria.AND);

			adminManagerRolesSV.queryCountBySpecificCondition(criteria);

			if (CollectionUtils.isNotEmpty(adminManagers)) {

				AdminManager adminManager = adminManagers.get(Constant.ZERO);
				// TODO
				// if (StringUtils.isNotBlank(AdminManager.getMenus())) {
				// resource = Sets.newHashSet(AdminManager.getMenus().split(
				// ","));
				// }
			}

			modelMap.addAttribute("rows",
					splitPermissions(permissions, resource));
			modelMap.addAttribute("targets", adminManagers);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "adminAlone:author.author";
	}

	@RequestMapping("/author")
	@ResponseBody
	public DataView authority(String sid, String auths,
			final HttpServletRequest request, ModelMap modelMap) {
		
		DataView dataView = new DataView() ;
		
		try {
			if (StringUtils.isNotBlank(sid) && StringUtils.isNotBlank(auths)) {

				String[] ids = sid.split(",");
				for (String id : ids) {
					AdminManager user = new AdminManager();
					user.setManagerId(Long.valueOf(id));
					// user.setMenus(auths);

					adminManagerSV.update(user);
				}

				dataView.setCode(Constant.SUCCESS);
				dataView.setMessage("操作成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}
}
